---
<!-- Copyright © SixtyFPS GmbH <info@slint.dev> ; SPDX-License-Identifier: MIT -->
title: FocusScope
description: FocusScope element api.
---
import SlintProperty  from '/src/components/SlintProperty.astro';
import Link from '/src/components/Link.astro';

```slint playground
export component Example inherits Window {
    width: 100px;
    height: 100px;
    forward-focus: my-key-handler;
    my-key-handler := FocusScope {
        key-pressed(event) => {
            debug(event.text);
            if (event.modifiers.control) {
                debug("control was pressed during this event");
            }
            if (event.text == Key.Escape) {
                debug("Esc key was pressed")
            }
            accept
        }
    }
}
```

The `FocusScope` exposes callbacks to intercept key events. Note that `FocusScope`
will only invoke them when it `has-focus`.

The <Link type="KeyEvent" /> has a text property, which is a character of the key entered.
When a non-printable key is pressed, the character will be either a control character,
or it will be mapped to a private unicode character. The mapping of these non-printable, special characters is available in the <Link type="KeyEvent"/> namespace

## Properties

### has-focus
<SlintProperty propName="has-focus" typeName="bool" defaultValue="false" propertyVisibility="out">
Is `true` when the element has keyboard focus.
</SlintProperty>

### enabled
<SlintProperty propName="enabled" typeName="bool" defaultValue="true">
 When true, the `FocusScope` will make itself the focused element when clicked. Set this to false if you don't want the click-to-focus
    behavior. Similarly, a disabled `FocusScope` does not accept the focus via tab focus traversal. A parent `FocusScope` will still receive key events from
    child `FocusScope`s that were rejected, even if `enabled` is set to false.
</SlintProperty>

## Functions

### focus()
Call this function to transfer keyboard focus to this `FocusScope`, to receive future <Link type="KeyEvent" />s.

### clear-focus()
Call this function to remove keyboard focus from this `FocusScope` if it currently has the focus. See also <Link type="FocusHandling" />.

## Callbacks

### key-pressed(KeyEvent) -> EventResult
Invoked when a key is pressed, the argument is a <Link type="KeyEvent" /> struct. The returned `EventResult`
indicates whether to accept or reject the event. Rejected events are forwarded to the parent element.

### key-released(KeyEvent) -> EventResult
Invoked when a key is released, the argument is a <Link type="KeyEvent" /> struct. The returned `EventResult`
indicates whether to accept or reject the event. Rejected events are forwarded to the parent element.

### focus-changed-event()
Invoked when the focus on the `FocusScope` has changed.
